package com.bosssoft.trainee.controller;

import com.bosssoft.trainee.common.annotation.Description;
import com.bosssoft.trainee.mbg.result.CommonResult;
import com.bosssoft.trainee.mbg.vo.UserVO;
import com.bosssoft.trainee.security.bo.UserBO;
import com.bosssoft.trainee.service.ResourceService;
import com.bosssoft.trainee.service.RoleService;
import com.bosssoft.trainee.service.UserService;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;

/**
 * @Author: wuxin
 * @Project: rbac-SpringCloud
 * @Date: 2022/7/14
 * @Description:
 */
@RestController
@RequestMapping("/api")
@Validated
public class ApiController {
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @Autowired
    ResourceService resourceService;

    @Description("网关调用-获取UserDetails")
    @GetMapping(value = "/loadUserByUsername")
    public UserBO loadUserByUsername(@NotBlank(message = "用户名不能为空")
                                     @RequestParam("username") String username) {
        return userService.loadUserByUsername(username);
    }

    @Description("网关调用-检查工号是否被占用")
    @GetMapping(value = "/checkWorkerNum")
    public CommonResult<Boolean> checkWorkerNum(@NotBlank(message = "工号不能为空")
                                                @Pattern(regexp = "^[A-Z]{2}\\d{6}$", message = "工号格式不正确")
                                                @RequestParam("workerNum")
                                                String workerNum,
                                                @RequestParam(value = "id", required = false)
                                                Integer id) {
        return CommonResult.success(userService.checkWorkerNum(workerNum, id));
    }

    @Description("网关调用-检查用户名是否被占用")
    @GetMapping(value = "/checkUsername")
    public CommonResult<Boolean> checkUsername(@NotBlank(message = "用户名不能为空")
                                               @Length(min = 4, max = 16, message = "用户名长度在 4 到 16 个字符")
                                               @Pattern(regexp = "^[a-zA-Z\\d_-]{4,16}$", message = "用户名只能由字母、数字、\"_\"、\"-\"组成")
                                               @RequestParam("username") String username,
                                               @RequestParam(value = "id", required = false)
                                               Integer id) {
        return CommonResult.success(userService.checkUsername(username,id));
    }

    @Description("网关调用-检查角色代码是否被占用")
    @GetMapping(value = "/checkRoleCode")
    public CommonResult<Boolean> checkRoleCode(@NotBlank(message = "角色代码不能为空")
                                               @Pattern(regexp = "^\\d{4}$", message = "角色代码格式不正确")
                                               @RequestParam("code") String code,
                                               @RequestParam(value = "id", required = false)
                                               Integer id) {
        return CommonResult.success(roleService.checkCode(code,id));
    }

    @Description("网关调用-检查资源代码")
    @GetMapping(value = "/checkResoCode")
    public CommonResult<Boolean> checkResoCode(@NotBlank(message = "不能为空")
                                               @Pattern(regexp = "^\\d{4}$", message = "格式不正确")
                                               @RequestParam("code") String code,
                                               @RequestParam(value = "id", required = false)
                                               Integer id) {
        return CommonResult.success(resourceService.checkCode(code,id));
    }

    @Description("网关调用-检查资源Api是否被占用")
    @GetMapping(value = "/checkApi")
    public CommonResult<Boolean> checkApi(@NotBlank(message = "不能为空")
                                          @Length(max = 10, message = "资源url长度不得多于10个字符")
                                          @Pattern(regexp = "^((?=[a-zA-Z/]).){1,10}$", message = "格式不正确")
                                          @RequestParam("api") String api,
                                          @RequestParam(value = "id", required = false)
                                          Integer id) {

        return CommonResult.success(resourceService.checkCode(api,id));
    }

    @Description("网关调用-检查资源Url是否被占用")
    @GetMapping(value = "/checkUrl")
    public CommonResult<Boolean> checkUrl(@NotBlank(message = "不能为空")
                                          @Length(max = 10, message = "资源url长度不得多于10个字符")
                                          @Pattern(regexp = "^((?=[a-zA-Z/]).){1,10}$", message = "格式不正确")
                                          @RequestParam("url") String url,
                                          @RequestParam(value = "id", required = false)
                                          Integer id) {
        return CommonResult.success(resourceService.checkCode(url,id));
    }

    @Description("网关调用-根据Token获取获取用户信息")
    @GetMapping("/info")
    public UserVO getUSerInfo(@RequestParam("token") String token) {
        return userService.getUserInfo(token);
    }
}
